package Java.view.game.douniu;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import javafx.animation.TranslateTransition;
import javafx.util.Duration;
import javafx.scene.Node;
public class PokerController {
    //移动扑克牌
    public static void move(Poker poker, Point from, Point to) {
        // 创建TranslateTransition动画对象
        TranslateTransition transition = new TranslateTransition(Duration.millis(500), poker);
        // 设置动画的起始位置
        transition.setFromX(from.x);
        transition.setFromY(from.y);
        // 设置动画的结束位置
        transition.setToX(to.x);
        transition.setToY(to.y);
        // 启动动画
        transition.play();
    }
    // 计算是否能形成整十数
    public static boolean canMakeTen(ArrayList<Poker> hand) {
        // 计算所有组合的点数之和
        for (int i = 0; i < hand.size(); i++) {
            for (int j = i + 1; j < hand.size(); j++) {
                for (int k = j + 1; k < hand.size(); k++) {
                    int sum = hand.get(i).getValue() + hand.get(j).getValue() + hand.get(k).getValue();
                    if (sum % 10 == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    // 电脑玩家自动控制下注
    public static int autoBet(Player player, ArrayList<Poker> hand, int happyDou) {
        int bet = 0;
        if (canMakeTen(hand)) {
            bet = 200 + (int) (Math.random() * 201); // 随机下注200到400之间
        } else {
            bet = (int) (Math.random() * 151); // 随机下注0到150之间
        }
        System.out.println("玩家" + player.getName() + "下注了" + bet + "欢乐豆");
        return happyDou+bet;
    }
    //计算最佳得分
    public static int calculateBestScore(ArrayList<Poker> hand) {
        ArrayList<Integer> scores = new ArrayList<>();
        // 计算所有可能的三张牌的组合
        for (int i = 0; i < hand.size(); i++) {
            for (int j = i + 1; j < hand.size(); j++) {
                for (int k = j + 1; k < hand.size(); k++) {
                    int sum = hand.get(i).getValue() + hand.get(j).getValue() + hand.get(k).getValue();
                    // 确保三张牌的和是整十数
                    if (sum % 10 == 0) {
                        int score = 0;
                        // 计算不在组合中的牌的分数
                        for (int m = 0; m < hand.size(); m++) {
                            if (m != i && m != j && m != k) {
                                score += Math.min(hand.get(m).getValue(), 10);
                            }
                        }
                        scores.add(score);
                    }
                }
            }
        }
        return scores.isEmpty() ? 0 : Collections.max(scores);
    }
    // 电脑玩家自动计算最佳得分
    public static void autoCalculateBestScore(Player player, ArrayList<Poker> hand) {
        int bestScore = calculateBestScore(hand);
        player.setScore(bestScore);
    }
    // 辅助方法，用于根据花色数量计算花色的等级
    public static int getSuitRank(ArrayList<Poker> hand) {
        int rank = 0;
        for (Poker poker : hand) {
            int huaSe = poker.getHuaSe(); // 获取花色值
            switch (huaSe) {
                case 2: // 黑桃
                    rank += 4;
                    break;
                case 0: // 红桃
                    rank += 3;
                    break;
                case 1: // 梅花
                    rank += 2;
                    break;
                case 3: // 方片
                    rank += 1;
                    break;
                default:
                    throw new IllegalArgumentException("未知的花色值: " + huaSe);
            }
        }
        return rank;
    }
}